﻿using Kevin.Comm.Utility;
using Kevin.Comm.Web;
using Kevin.Service.Client.Restful;
using Kevin.Service.Common;
using Kevin.Shop.Business.Comments.Components;
using Kevin.Shop.Business.Core.Components;
using Kevin.Shop.Business.Core.Configuration;
using Kevin.Shop.Business.Products.Components;
using Kevin.Shop.Business.Products.Components.Category;
using Kevin.Shop.Business.Service.Entity.Helper;
using RiderShop.Mobile.Common.Component;
using RiderShop.Mobile.Controllers;
using RiderShop.Mobile.Modle;
using RiderShop.Mobile.ServiceEntity;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace RiderShop.Mobile.Controllers
{
    public class SortController : ThemeController
    {
        #region CategoryList
         //<summary>
         //一级分类和二级分类
         //</summary>
         //<returns></returns>
        //public ActionResult CategoryList()
        //{
        //    var defList = BuildList();
        //    if (defList.Count > 0)
        //    {
        //        //return View(defList);
        //        return View(WebClientContext.Current.SiteTheme, "Sort", "CategoryList", "_MobileLayout", defList);
        //    }
        //    else
        //    {
        //        return View(WebClientContext.Current.SiteTheme, "Sort", "CategoryList", "_MobileLayout", null);
        //    }
        //}

        private static List<CategoryInfoExtend> BuildList()
        {
            var defList = new List<CategoryInfoExtend>();
            CategoryModel model = new CategoryModel();
            model.LoadAllSubcategory();
            var groupList = model.Groups;
            var categoryList = model.Categories;
            var allSubcategory = model.SubCategories;
            //构建产品一级分类和产品二级分类的树结构
            if (WebClientContext.Current.CategorySwitch == WebClientContext.StrGroup)
            {
                for (int i = 0; i < groupList.Count(); i++)
                {
                    var group = new CategoryInfoExtend();
                    group.CategoryId = groupList[i].CategoryId;
                    group.CategoryName = groupList[i].CategoryName;
                    group.CategoryList =
                        categoryList.Where(
                            p =>
                            (p.Depth == CategoryLevelType.Category && p.ParentCategoryId == groupList[i].CategoryId)).ToList();
                    defList.Add(group);
                }
            }
            else if (WebClientContext.Current.CategorySwitch == WebClientContext.StrCategory)
            {
                foreach (var categoryInfo in categoryList)
                {
                    var category = new CategoryInfoExtend();
                    category.CategoryId = categoryInfo.CategoryId;
                    category.CategoryName = categoryInfo.CategoryName;
                    category.CategoryList =
                        allSubcategory.Where(
                            p =>
                            (p.Depth == CategoryLevelType.SubCategory && p.ParentCategoryId == categoryInfo.CategoryId)).ToList();
                    defList.Add(category);
                }
            }
            return defList;
        }

        #endregion

        /// <summary>
        /// 得到全部等级的的分类
        /// </summary>
        /// <returns></returns>
        public ActionResult CategoryList()
        {
            CategoryModel model = new CategoryModel();
            model.LoadAllSubcategory();
            //转化为json格式的字符串
            model.AllSubCategoriesJSON = Kevin.Comm.Utility.Serializer.ConvertObjectToJson(model.SubCategories);
            if (model.SubCategories.Count() > 0)
            {
                return View(WebClientContext.Current.SiteTheme, "Sort", "CategoryList", WebClientContext.Current.CacheKeyDefaultMasterPage, model);
            }
            return View(WebClientContext.Current.SiteTheme, "Sort", "CategoryList", WebClientContext.Current.CacheKeyDefaultMasterPage, model);
        }

        public ActionResult GetProducts(int categoryId)
        {
            var query = new ProductQuery();
            query.CategoryId = categoryId;
            query.IsWebsiteSearch = true;
            query.PageSize = 30;
            query.SortOrder = SortAction.Desc;
            query.SortBy = ProductQuery.ProductSortColumn.TotalSoldQty.ToString();
            DbQueryResult<List<RiderShop.Mobile.ServiceEntity.Product.ProductBasicInfoExtend>> productList = RestfulHelper.Query<RiderShop.Mobile.ServiceEntity.Product.ProductBasicInfoExtend>(SiteUrl.FormatUrl("ProductManagement_GetProductInfo"), query);

            return Json(productList.Data, JsonRequestBehavior.AllowGet);
        }

        #region CategoryListPartialView
        /// <summary>
        /// 一级分类和二级分类
        /// </summary>
        /// <returns></returns>
        public ActionResult CategoryListPartialView()
        {
            var defList = BuildList();
            return PartialView(defList);
        }

        /// <summary>
        /// 一级分类和二级分类
        /// </summary>
        /// <returns></returns>
        public ActionResult CategoryPanelPartialView()
        {
            var defList = BuildList();
            return PartialView(defList);
        }
        #endregion

        #region SecondLevelCategoryList
        /// <summary>
        /// 根据一级分类编号得到二级分类
        /// </summary>
        /// <param name="id">一级分类编号</param>
        /// <param name="name">一级分类名称</param>
        /// <returns></returns>
        public ActionResult SecondLevelCategoryList(int id, string name)
        {
            CategoryModel model = new CategoryModel();
            model.LoadAllSubcategory();
            List<CategoryInfo> categories = model.Categories;
            List<CategoryInfo> categoryList = categories.Where(
                                                p =>
                                              (p.Depth == CategoryLevelType.Category && p.ParentCategoryId == id)).ToList();
            //ViewBag.CategoryName = categoryList[0].ParentCategoryName;
            
            CategoryInfo category = new CategoryInfo();
            category.CategoryName = "所有产品";
            category.CategoryId = id;
            categoryList.Insert(0, category);

            model.Categories = categoryList;

            //var list = categoryList.Where(P => P.ParentCategoryId == id || P.CategoryName == "所有产品").ToArray();
            if (categoryList.Count > 0)
            {
                //return View(defList);
                return View(WebClientContext.Current.SiteTheme, "Sort", "SecondLevelCategoryList", "_MobileLayout", model);
            }
            else
            {
                return View(WebClientContext.Current.SiteTheme, "Sort", "SecondLevelCategoryList", "_MobileLayout", null);
            }
        }
        #endregion

        #region SubCategoryList
        /// <summary>
        /// 根据二级分类的编号得到三级分类
        /// </summary>
        /// <param name="id">二级分类编号</param>
        /// <param name="name">二级分类名称</param>
        /// <returns></returns>
        public ActionResult SubCategoryList(int id, string name)
        {
            CategoryModel model = new CategoryModel();
            model.LoadAllSubcategory();
            List<CategoryInfo> subCategories = model.SubCategories;
            List<CategoryInfo> subCategoryList = subCategories.Where(
                                                  p =>
                                                 (p.Depth == CategoryLevelType.SubCategory && p.ParentCategoryId == id)).ToList();
            //ViewBag.CategoryName = subCategoryList[0].ParentCategoryName;
            CategoryInfo category = new CategoryInfo();
            category.CategoryName = "所有产品";
            category.CategoryId = id;
            subCategoryList.Insert(0, category);

            model.SubCategories = subCategoryList;

            //var list = subCategoryList.Where(P => P.ParentCategoryId == id || P.CategoryName == "所有产品").ToArray();
            if (subCategoryList.Count() > 0)
            {
                //return View(list);
                return View(WebClientContext.Current.SiteTheme, "Sort", "SubCategoryList", "_MobileLayout", model);
            }
            else
            {
                return View(WebClientContext.Current.SiteTheme, "Sort", "SubCategoryList", "_MobileLayout", null);
            }
        }
        #endregion
    }
}
